package ch.ti.kmltomap;

import org.xmlpull.v1.XmlPullParser;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.PolygonOptions;

public class Polygon extends Geometry{

	public LinearRing outerBoundaryIs = null;
	public LinearRing innerBoundaryId = null;
	
	public Polygon(){
		
	}
	
	public Polygon(XmlPullParser xpp){
		try{
			this.id = xpp.getAttributeValue(null, "id");
			int eventType = xpp.next();
			String tagName = "";
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if(eventType == XmlPullParser.START_TAG){
					tagName = xpp.getName();
					if(tagName.equals("outerBoundaryIs")){
						this.outerBoundaryIs = new LinearRing(xpp);
					}else if(tagName.equals("innerBoundaryId")){
						this.innerBoundaryId = new LinearRing(xpp);
					}					
				}
				if(eventType == XmlPullParser.TEXT){

				}else if(eventType == XmlPullParser.END_TAG){
					if(xpp.getName().equals("Polygon")){
						break;
					}
				}
				eventType = xpp.next();
			}
		}catch (Exception e) {
		}	
	}
	
	public void draw(GoogleMap map, String testo){
		PolygonOptions po = new PolygonOptions();
		if(this.innerBoundaryId != null){
			for (Coordinate coordinate : this.innerBoundaryId.coordinates().coordinates())
				po.add(coordinate.latLng());
		}
		if(this.outerBoundaryIs != null){
			for (Coordinate coordinate : this.outerBoundaryIs.coordinates().coordinates())
				po.add(coordinate.latLng());
		}
		map.addPolygon(po);
	}
	
}
